package com.ruoyi.business.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.IronCoreMapper;
import com.ruoyi.business.domain.IronCore;
import com.ruoyi.business.service.IIronCoreService;
import org.apache.commons.lang3.StringUtils;

/**
 * 定子查询Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-19
 */
@Service
public class IronCoreServiceImpl implements IIronCoreService 
{
    @Autowired
    private IronCoreMapper ironCoreMapper;

    /**
     * 查询定子查询
     * 
     * @param id 定子查询主键
     * @return 定子查询
     */
    @Override
    public IronCore selectIronCoreById(Long id)
    {
        return ironCoreMapper.selectIronCoreById(id);
    }

    /**
     * 查询定子查询列表
     * 
     * @param ironCore 定子查询
     * @return 定子查询
     */
    @Override
    public List<IronCore> selectIronCoreList(IronCore ironCore)
    {
        return ironCoreMapper.selectIronCoreList(ironCore);
    }

    /**
     * 新增定子查询
     * 
     * @param ironCore 定子查询
     * @return 结果
     */
    @Override
    public int insertIronCore(IronCore ironCore)
    {
        ironCore.setCreateTime(DateUtils.getNowDate());
        
        // 如果编号为空，自动生成编号
        if (StringUtils.isEmpty(ironCore.getCode()))
        {
            // 生成格式为：IC + 年月日 + 4位序号，例如：IC2025071000001
            String dateCode = DateUtils.dateTimeNow("yyyyMMdd");
            String prefix = "IC" + dateCode;
            
            // 查询当天最大编号
            IronCore queryCore = new IronCore();
            queryCore.setCode(prefix);
            List<IronCore> list = ironCoreMapper.selectIronCoreList(queryCore);
            
            int sequence = 1;
            if (list != null && list.size() > 0) {
                // 找出当天最大序号
                for (IronCore core : list) {
                    String existCode = core.getCode();
                    if (existCode != null && existCode.startsWith(prefix)) {
                        try {
                            String sequenceStr = existCode.substring(prefix.length());
                            int currentSequence = Integer.parseInt(sequenceStr);
                            if (currentSequence >= sequence) {
                                sequence = currentSequence + 1;
                            }
                        } catch (Exception e) {
                            // 忽略解析错误
                        }
                    }
                }
            }
            
            // 生成新编号
            String newCode = prefix + String.format("%05d", sequence);
            ironCore.setCode(newCode);
        }
        
        return ironCoreMapper.insertIronCore(ironCore);
    }

    /**
     * 修改定子查询
     * 
     * @param ironCore 定子查询
     * @return 结果
     */
    @Override
    public int updateIronCore(IronCore ironCore)
    {
        ironCore.setUpdateTime(DateUtils.getNowDate());
        return ironCoreMapper.updateIronCore(ironCore);
    }

    /**
     * 批量删除定子查询
     * 
     * @param ids 需要删除的定子查询主键
     * @return 结果
     */
    @Override
    public int deleteIronCoreByIds(Long[] ids)
    {
        return ironCoreMapper.deleteIronCoreByIds(ids);
    }

    /**
     * 删除定子查询信息
     * 
     * @param id 定子查询主键
     * @return 结果
     */
    @Override
    public int deleteIronCoreById(Long id)
    {
        return ironCoreMapper.deleteIronCoreById(id);
    }
}
